package com.guoyw.seata.dubbo.order.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger2API文档的配置
 * author: guoyw
 * create: 2020-07-21 17:08
 **/
//@Configuration
//@EnableSwagger2WebMvc
public class Swagger2Config{
  
  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
      .apiInfo(apiInfo())
      .select()
      .apis(RequestHandlerSelectors.basePackage("com.guoyw.seata.dubbo.order"))
      .paths(PathSelectors.any())
      .build()
      .securitySchemes(securitySchemes())
      .securityContexts(securityContexts());
  }
  
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
      .title("订单")
      .description("订单")
      .termsOfServiceUrl("https://gitee.com/gw-public")
      .version("1.0")
      .build();
  }
  
  private List<ApiKey> securitySchemes() {
    //设置请求头信息
    List<ApiKey> result = new ArrayList<>();
    //参数1：字段的名字，参数2：字段的键，参数3：参数位置
    ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header");
    result.add(apiKey);
    return result;
  }
  
  //认证的上下文，这里面需要指定哪些接口需要认证
  private List<SecurityContext> securityContexts() {
    //设置需要登录认证的路径
    List<SecurityContext> result = new ArrayList<>();
    result.add(getContextByPath("/member/.*"));
    result.add(getContextByPath("/cart/.*"));
    result.add(getContextByPath("/order/.*"));
    result.add(getContextByPath("/returnApply/.*"));
    return result;
  }
  private SecurityContext getContextByPath(String pathRegex) {
    return SecurityContext.builder()
      .securityReferences(defaultAuth())
      .forPaths(PathSelectors.regex(pathRegex))
      .build();
  }
  
  //这个方法是验证的作用域
  private List<SecurityReference> defaultAuth() {
    List<SecurityReference> result = new ArrayList<>();
    AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
    AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
    authorizationScopes[0] = authorizationScope;
    result.add(new SecurityReference("Authorization", authorizationScopes));
    return result;
  }
  
}
